<template>
	<view class="" v-if="time<time2"></view>
	<view v-else class="login" :style="{paddingTop: height + 'px'}">
		<view class="flex-c login_title">
			<view class="cuIcon-close" @click="handleclose"></view>
			<view>微信授权登录</view>
		</view>
		<view class="img">
			<image src="/static/logo.png" mode="widthFix"></image>
		</view>
		<button class="button flex-c" type=" " @click="onWxLogin">微信授权登陆</button>
		<button class="button button2 flex-c" @click="handleback">取消</button>
		<view class="text flex align-center">
			<image src="/static/gou.png" v-if="isclose == 1" mode="scaleToFill" @click="handleTick"></image>
			<image src="/static/gou2.png" v-if="isclose == 0 " mode="scaleToFill" @click="handleTick"></image>
			<view class="flex align-center">阅读并同意<navigator url="../text/text?t=0">《用户协议》</navigator>和<navigator
					url="../text/text?t=1">《隐私政策》</navigator>
			</view>
		</view>
		<view class="modal_view" v-if="isphone">
			<view class="fooot bg-white">
				<view class="A">获取授权</view>
				<view class="B">获取手机号的微信授权</view>
				<button class="flex-c" open-type="getPhoneNumber" @getphonenumber="phone">获取微信手机号</button>
			</view>
		</view>
	</view>
</template>

<script>
	// import {
	// 	mutations as uniIdMutations
	// } from '@/uni_modules/uni-id-pages/common/store.js';
	// import uniImUtils from '@/uni_modules/uni-im/common/utils.js';
	export default {
		data() {
			return {
				isclose: 0,
				height: 0,
				openid: '',
				isshow: 0,
				isphone: 0,
				user: '',
				id: null,
				mch_id: null,
				time: '',
				time2: ''
			}
		},
		onLoad(e) {
			console.log("进入登录页id", e)
			this.time = Date.parse(new Date()) / 1000
			this.time2 = this.$time
			if (this.time < this.time2) {
				uni.navigateTo({
					url: '/pages/index'
				})
			}
			if (e.id) {
				console.log("进入了id", e)
				this.id = e.id
			}
			if (getApp().globalData.direct_uid != 0) {
				this.id = getApp().globalData.direct_uid
				console.log("登录页", this.id)
			}
			if (getApp().globalData.mch_id != 0) {
				this.mch_id = getApp().globalData.mch_id
				console.log("登录页==============", this.mch_id)
			}
			console.log("id赋值结束", this.id)
			if (e.mch_id) {
				this.mch_id = e.mch_id
			}
			uni.getStorage({
				key: 'height',
				success: (res) => {
					this.height = res.data
				}
			})
		},
		created() {
			uni.setStorageSync("isLogin", 1)
		},
		methods: {
			
			onWxLogin() {
				let than = this
				let self = this;
				if (this.isclose == 0) {
					this.$utils.tips("请仔细阅读并同意《用户协议》和《隐私协议》")
					return
				}
				if (this.isshow == 0) {
					uni.login({
						provider: 'weixin',
						success: function(res) {
							console.log(res, '微信');
							self.$api.login({
								code: res.code,
								mch_id: self.mch_id,
								direct_uid: self.id
							}, true).then(res => {
								if (res.code == 200001) {
									uni.setStorageSync("expires_in", res.data.expires_in);
									uni.setStorageSync("token_type", res.data.token_type);
									uni.setStorageSync("access_token", res.data.access_token);
									self.user = res.data.user
									if (!self.user.phone) {
										self.isphone = 1
									} else {
										uni.setStorageSync("userinfo", self.user);
										uni.setStorageSync("userSig", res.data.sig);
										self.$utils.tips("登陆成功")
										// 聊天登录
										than.log(res)
										// self.online();
										setTimeout(() => {
											uni.navigateBack({
												delta: 1,
												animationDuration: 200
											})
										}, 500);
									}
								}
							});
						}
					});
				} else {

				}

			},
			async log(res) {
				console.log(res, '用户信息：：：');
				let query = {
					userID: res.data.user.id.toString(),
					userSig: res.data.sig.toString()
				}
				uni.$TUIKit.login(query)
					.then(function(imResponse) {
						console.log(imResponse.data, '登录成功！'); // 登录成功
						if (imResponse.data.repeatLogin === true) {

							// 标识帐号已登录，本次登录操作为重复登录。v2.5.1 起支持
							console.log(imResponse.data.errorInfo);
						}
					})
					.catch(function(imError) {
						console.warn('login error:', imError); // 登录失败的相关信息
					});

				// const uniImCo = uniCloud.importObject("uni-im-co", {
				// 	customUI: true
				// })

				// let data = await uniImCo.loginWithJWT(res.data.signedData)
				// if (data.errCode) {
				// 	console.log(data.errCode,'错了：：');
				// }
				// console.log(data,'loginWithJWT');
				// this.$api.imid({
				// 	im_id: data.uid
				// }).then(res => {
				// 	console.log(res, 'imid::::');
				// 	if (res.code == 200001) {
				// 		console.log(res, '上传im_id');
				// 	}
				// })
				// await uniIdMutations.updateUserInfo()
			},
			online() {
				console.log("=======client_id========", uni.getStorageSync("client_id"))
				this.$api.online({
					client_id: uni.getStorageSync("client_id")
				}, 1).then(res => {
					if (res && res.code == 200001) {
						// getApp().globalData.onlogin = 1
						console.log("上线成功", res)
					} else {
						console.log("已经离线", res)

					}
				})
			},
			handleback() {
				this.$utils.back(2)
			},
			handleTick() {
				this.isclose = !this.isclose
			},
			phone(e) {
				let self = this
				self.$api.getphone({
					code: e.detail.code
				}, true).then(res => {
					if (res.code == 200001) {
						self.user.phone = res.data
						self.isphone = 0
						uni.setStorageSync("userinfo", self.user);
						self.$utils.tips("登陆成功")
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/tabbar/index',
							})
						}, 500);
					}
				});
			},
			handleclose() {
				uni.navigateBack({
					delta: 1,
					animationDuration: 500
				})
			},
		},


		onShow() {},


	}
</script>

<style lang="scss">
	.login {
		background-color: #fff;
		min-height: 100vh;
		min-width: 100vw;

		.login_title {
			height: 89rpx;
			font-size: 36rpx;
			color: #333;
			font-weight: bold;
			position: relative;

			.cuIcon-close {
				position: absolute;
				top: 20rpx;
				left: 20rpx;
				color: #999;
			}
		}
	}

	.img {
		width: 180rpx;
		height: 180rpx;
		margin: auto;
		margin-top: 109rpx;
	}

	.button {
		width: 622rpx;
		height: 98rpx;
		background: #1CD699;
		border-radius: 49rpx;
		font-size: 30rpx;
		color: #fff;
		margin-top: 171rpx;
	}

	.button2 {
		background: #E5E5E5;
		margin-top: 60rpx;
	}

	.text {
		padding-left: 66rpx;
		padding-right: 53rpx;
		position: absolute;
		bottom: 99rpx;

		image {
			width: 40rpx;
			height: 40rpx;
			margin-right: 13rpx;
		}
	}

	.fooot {
		position: fixed;
		bottom: 0;
		left: 0;
		height: 420rpx;
		width: 100%;
		border-radius: 20rpx 20rpx 0 0;
		text-align: center;
		padding: 50rpx 0;

		.A {
			font-size: 40rpx;
			color: #333;
		}

		.B {
			margin-top: 35rpx;
			margin-bottom: 80rpx;
		}

		button {
			width: 622rpx;
			height: 88rpx;
			background: #1CD699;
			border-radius: 44rpx;
			font-size: 35rpx;
			color: #fff;

		}
	}
</style>
